package com.modern.js.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Employee;
import com.modern.js.mapper.AssistPojoMapper;
import com.modern.js.mapper.EmployeeMapper;
import com.modern.js.pojo.AssistPojo;
import com.modern.js.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.modern.js.utils.MyContsant;
import com.modern.js.utils.StringUtils;
import io.swagger.models.auth.In;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AssistPojoMapper assistPojoMapper;


    @Override
    public List<AssistPojo> selectById(Integer empId) {
        List<AssistPojo> assistPojo = employeeMapper.selectById(empId);
        return assistPojo;
    }

//    添加功能
    @Override
    public int register(Employee employee,Integer roleId) {
        //生成一个随机数
        String salt= UUID.randomUUID().toString();
//        加密密码Sha256Hash
        String s = new Sha256Hash(employee.getPwd(),salt, MyContsant.INTERCOUNT).toBase64();
        System.out.println("加密的密码："+s);
        employee.setSalt(salt);
        employee.setPwd(s);
//        int result = employeeMapper.register(employee);
        employee.setCreateTime(StringUtils.getNowTime());
        employee.setUpdateTime(StringUtils.getNowTime());
        employee.setIsDel(1);
        int result = employeeMapper.insert(employee);
        int empId = employee.getEmpId();
        System.out.println("这是刚添加进去的数据："+empId);
        AssistPojo assistPojo = new AssistPojo();
        assistPojo.setRoleId(roleId);
        assistPojo.setEmpId(empId);
        assistPojo.setCreateTime(employee.getCreateTime());
        assistPojo.setUpdateTime(employee.getUpdateTime());
        assistPojo.setIsDel(1);
        result+=assistPojoMapper.addEmployeeRole(assistPojo);
        if (result==2){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }
        return result;
    }

    @Override
    public int employeeUpdate(Employee employee,Integer roleId,Integer deptId) {
        System.out.println("你好世界");
        //判断密码是否为空，如果不为空则执行下面if的语句，如果为空则执行else的语句
        if (!"".equals(employee.getPwd())){
            //生成一个随机数
            String salt= UUID.randomUUID().toString();
            //加密密码Sha256Hash
            String s = new Sha256Hash(employee.getPwd(),salt, MyContsant.INTERCOUNT).toBase64();
            employee.setPwd(s);
            employee.setSalt(salt);
        }else {
            employee.setPwd(null);
        }

        employee.setUpdateTime(StringUtils.getNowTime());
        int result = employeeMapper.updateById(employee);
        //修改员工与角色关系表的数据
       if (roleId!=null && deptId!=null){
           AssistPojo assistPojo = new AssistPojo();
           assistPojo.setRoleId(roleId);
           assistPojo.setUpdateTime(employee.getUpdateTime());
           assistPojo.setEmpId(employee.getEmpId());
           result += assistPojoMapper.updateEmployeeRole(assistPojo);

           assistPojo.setDeptId(deptId);
           result+=assistPojoMapper.updateDeptRole(assistPojo);
       }

        return result;
    }

//    查询全部员工信息的分页功能
    @Override
    public IPage<Employee> selectList(Page<Employee> page,String empName,String createTime) {
        return employeeMapper.selectList(page,empName,createTime);
    }

    @Override
    public Employee selectByName(String empName) {
        return employeeMapper.selectByName(empName);
    }

    @Override
    public int updatePerson(Employee employee) {

        if (!"".equals(employee.getPwd())){
            //生成一个随机数
            String salt= UUID.randomUUID().toString();
            //加密密码Sha256Hash
            String s = new Sha256Hash(employee.getPwd(),salt, MyContsant.INTERCOUNT).toBase64();
            employee.setPwd(s);
            employee.setSalt(salt);
        }else {
            employee.setPwd(null);
        }
        employee.setUpdateTime(StringUtils.getNowTime());
        int result = employeeMapper.updatePerson(employee);
        return result;
    }
}
